package com.example.demo.controller;

import com.example.demo.po.Article;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/article")
    @ResponseBody
    public List<Article> findAllArticle(){
        return articleService.findAllArticle();
    }


    @GetMapping("/index")
    public ModelAndView index(ModelAndView modelAndView){
        modelAndView.setViewName("index");

        List<Article> allArticle = articleService.findAllArticle();

        modelAndView.addObject("articles", allArticle);
        modelAndView.addObject("name", "cgw");

        return modelAndView;
    }
}
